<?php
namespace Emos\Editor;

class Node {
    protected $tree;
    
    protected $entity;
    protected $pkValue;
    protected $sub = false;
    
    protected $row = array();
    protected $fkName = null;

    public function __construct(Tree $tree, Entity $entity, $pkValue, $sub=false) {
        $this->tree     = $tree; 
        $this->entity   = $entity; 
        $this->pkValue  = $pkValue; 
        $this->sub      = !!$sub; 
    }
    
    public function getEntity() {
        return $this->entity;
    }

    public function getEntityName() {
        return $this->entity->name;
    }
    
    public function getPkValue() {
        return $this->pkValue;
    }

    public function isSub() {
        return $this->sub;
    }
    
    public function setRow($record) {
        $this->record = $record;
    }
    
    public function getRow() {
        return $this->record;
    }    
    
    public function equal(Node $node) {
        return 
            ($this->getEntity() === $node->getEntity())
            &&
            ($this->getPkValue() === $node->getPkValue());       
    }
    
    public function __toString() {
        return '['.$this->entity->getName().':'.$this->pkValue.'|FK='.$this->fkName.($this->isSub() ? '|SUB':'').']';
    }    
    
    public function setFkName($fkName) {
        $this->fkName = $fkName;
    }
    
    public function getFkName() {
        return $this->fkName;
    }
}